<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="上报人姓名" name="userName">
						<a-input v-model:value="searchFormState.userName" placeholder="请输入上报人姓名" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="上报人电话" name="userPhone">
						<a-input v-model:value="searchFormState.userPhone" placeholder="请输入上报人电话" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="隐患类型" name="hiddenType">
						<a-select v-model:value="searchFormState.hiddenType" placeholder="请选择隐患类型" :options="hiddenTypeOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="隐患等级" name="hiddenLevel">
						<a-select v-model:value="searchFormState.hiddenLevel" placeholder="请选择隐患等级" :options="hiddenLevelOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-if="hasPerm('hiddenReportBatchDelete')">
					<a-form-item label="整改部门" name="rectificationOrgId">
						<a-tree-select
							v-model:value="searchFormState.orgId"
							style="width: 100%"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							placeholder="请选择所属部门"
							allow-clear
							tree-default-expand-all
							:tree-data="treeData"
							:tree-default-expanded-keys="treeDefaultExpandedKeys"
							:field-names="{
										children: 'children',
										label: 'name',
										value: 'id'
									}"
							show-search
							tree-node-filter-prop="name"
						></a-tree-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="核实日期" name="verifyDate">
						<a-range-picker v-model:value="searchFormState.verifyDate" value-format="YYYY-MM-DD"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			bordered
			:row-key="(record) => record.reportId"
			:tool-config="toolConfig">
<!--			:scroll="{ x: 1200,y:500 }"-->

			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'hiddenType'">
					{{ $TOOL.dictTypeData('hiddenType', record.hiddenType) }}
				</template>
				<template v-if="column.dataIndex === 'hiddenLevel'">
					<a-tag color="cyan" v-if="record.hiddenLevel==null">暂未定级</a-tag>
					<a-tag color="green" v-if="record.hiddenLevel==0">一般问题</a-tag>
					<a-tag color="blue" v-if="record.hiddenLevel==1">一般隐患</a-tag>
					<a-tag color="orange" v-if="record.hiddenLevel==2">较大隐患</a-tag>
					<a-tag color="red" v-if="record.hiddenLevel==3">重大隐患</a-tag>
				</template>
				<template v-if="column.dataIndex === 'approval'">
					{{ $TOOL.dictTypeData('hiddenStatus', record.approval) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="allocateVerifyFormRef.onOpen(record)" v-if="record.approval==0">分配核实人</a>
						<a @click="verifyFormRef.onOpen(record)" v-if="record.approval==1">核实信息</a>
						<a @click="mgrAuditFormRef.onOpen(record)" v-if="record.approval==2">隐患定级</a>
						<a @click="superviseAuditFormRef.onOpen(record)" v-if="record.approval==3">审核</a>
						<a @click="bizAuditFormRef.onOpen(record)" v-if="record.approval==4">审核</a>
						<a @click="safetyAuditFormRef.onOpen(record)" v-if="record.approval==5">审核</a>
						<a @click="chargeAuditFormRef.onOpen(record)" v-if="record.approval==6">审核</a>
						<a @click="mainAuditFormRef.onOpen(record)" v-if="record.approval==7">审核</a>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<AllocateVerifyForm ref="allocateVerifyFormRef" @successful="table.refresh(true)" />
	<VerifyForm ref="verifyFormRef" @successful="table.refresh(true)" />
	<MgrAuditForm ref="mgrAuditFormRef" @successful="table.refresh(true)" />
	<SuperviseAuditForm ref="superviseAuditFormRef" @successful="table.refresh(true)" />
	<BizAuditForm ref="bizAuditFormRef" @successful="table.refresh(true)" />
	<SafetyAuditForm ref="safetyAuditFormRef" @successful="table.refresh(true)" />
	<ChargeAuditForm ref="chargeAuditFormRef" @successful="table.refresh(true)" />
	<MainAuditForm ref="mainAuditFormRef" @successful="table.refresh(true)" />
</template>

<script setup name="hiddenreport">
	import tool from '@/utils/tool'
	import AllocateVerifyForm from './allocateverify.vue'
	import VerifyForm from './verifyaudit.vue'
	import MgrAuditForm from './mgraudit.vue'
	import SuperviseAuditForm from './superviseaudit.vue'
	import BizAuditForm from './bizaudit.vue'
	import SafetyAuditForm from './safetyaudit.vue'
	import ChargeAuditForm from './chargeaudit.vue'
	import MainAuditForm from './mainaudit.vue'
	import hiddenReportApi from '@/api/biz/hidden/hiddenReportApi'
	import bizUserApi from "@/api/biz/bizUserApi"
	let searchFormState = reactive({})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()


	const treeData = ref([])
	const treeDefaultExpandedKeys = ref([])
	const allocateVerifyFormRef = ref()
	const verifyFormRef = ref()
	const mgrAuditFormRef = ref()
	const superviseAuditFormRef = ref()
	const bizAuditFormRef = ref()
	const safetyAuditFormRef = ref()
	const chargeAuditFormRef = ref()
	const mainAuditFormRef = ref()

	const hiddenTypeOptions = tool.dictList('hiddenType')
	const hiddenLevelOptions = tool.dictList('hiddenLevel')
	const approvalOptions = tool.dictList('hiddenStatus')

	// 机构选择器数据
	bizUserApi.userOrgTreeSelector().then((res) => {
		if (res !== null) {
			treeData.value = res
			// 默认展开2级
			treeData.value.forEach((item) => {
				// 因为0的顶级
				if (item.parentId === '0') {
					treeDefaultExpandedKeys.value.push(item.id)
					// 取到下级ID
					if (item.children) {
						item.children.forEach((items) => {
							treeDefaultExpandedKeys.value.push(items.id)
						})
					}
				}
			})
		}
	})

	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '上报人姓名',
			dataIndex: 'userName',
			width: 100,
			align:'center'
		},
		{
			title: '上报人电话',
			dataIndex: 'userPhone',
			width: 120,
			align:'center'
		},
		{
			title: '隐患类型',
			dataIndex: 'hiddenType',
			align:'center'
		},
		{
			title: '隐患等级',
			dataIndex: 'hiddenLevel',
			align:'center'
		},
		// {
		// 	title: '隐患描述',
		// 	dataIndex: 'hiddenRemark',
		// 	align:'center'
		// },
		// {
		// 	title: '当前位置',
		// 	dataIndex: 'address',
		// 	width: 240,
		// 	align:'center'
		// },
		{
			title: '审批状态',
			dataIndex: 'approval',
			align:'center'
		},
		{
			title: '建议奖励金额',
			dataIndex: 'suggestionRewards',
			align:'center'
		},
		// {
		// 	title: '实际奖励金额',
		// 	dataIndex: 'hiddenRewards',
		// 	align:'center'
		// },
		{
			title: '核实人姓名',
			dataIndex: 'verifyUserName',
			align:'center'
		},
		{
			title: '核实时间',
			dataIndex: 'verifyTime',
			width: 180,
			align:'center'
		},
		// {
		// 	title: '隐患具体地址',
		// 	dataIndex: 'hiddenAddr',
		// 	width: 240,
		// 	align:'center'
		// },
		{
			title: '整改部门名称',
			dataIndex: 'rectificationOrgName',
			align:'center'
		},
		{
			title: '整改期限',
			dataIndex: 'rectificationPeriod',
			align:'center'
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '150px'
		},
	]

	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		if (searchFormParam.verifyDate) {
			searchFormParam.verifyDateStart = searchFormParam.verifyDate[0]
			searchFormParam.verifyDateEnd = searchFormParam.verifyDate[1]
			delete searchFormParam.verifyDate
		}
		return hiddenReportApi.waitAuditPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
</script>
